class Navbar {
    static init() {
        if (!document.querySelector('.main-nav')) {
            const navHTML = `
                <nav class="main-nav">
                    <div class="logo">
                        <div class="logo-icon">
                            <i class="fas fa-seedling"></i>
                        </div>
                        <span>苗情灾情监测系统</span>
                    </div>
                    <ul class="nav-links">
                        <li><a href="index.html">首页</a></li>
                        <li><a href="growth.html">作物生长监测</a></li>
                        <li><a href="disaster.html">灾情监测</a></li>
                        <li><a href="video.html">视频监测</a></li>
                        <li><a href="remote-sensing.html">遥感分析</a></li>
                    </ul>
                    <div class="user-info">
                        <span class="username">${localStorage.getItem('savedUsername') || '用户'}</span>
                        <button class="logout-btn" onclick="Navbar.handleLogout()">
                            <i class="fas fa-sign-out-alt"></i>
                            退出
                        </button>
                    </div>
                </nav>
            `;
            
            document.body.insertAdjacentHTML('afterbegin', navHTML);
        }
        
        // 检查登录状态
        this.checkLoginStatus();
        this.setActiveLink();
    }

    // 检查登录状态
    static checkLoginStatus() {
        const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
        if (!isLoggedIn && !window.location.href.includes('login.html')) {
            window.location.href = 'login.html';
        }
    }

    // 处理退出登录
    static handleLogout() {
        if (confirm('确定要退出系统吗？')) {
            // 清除登录状态
            localStorage.removeItem('isLoggedIn');
            
            // 如果没有勾选记住密码，则清除保存的用户信息
            if (localStorage.getItem('rememberMe') !== 'true') {
                localStorage.removeItem('savedUsername');
                localStorage.removeItem('savedPassword');
                localStorage.removeItem('rememberMe');
            }
            
            // 跳转到登录页面
            window.location.href = 'login.html';
        }
    }

    static setActiveLink() {
        const currentPage = window.location.pathname.split('/').pop() || 'index.html';
        document.querySelectorAll('.nav-links a').forEach(link => {
            if (link.getAttribute('href') === currentPage) {
                link.classList.add('active');
            } else {
                link.classList.remove('active');
            }
        });
    }
} 